package utility_cry

import (
	"context"
	"github.com/gogf/gf/v2/crypto/gaes"
	"github.com/gogf/gf/v2/encoding/gbase64"
	"github.com/gogf/gf/v2/frame/g"
)

func AesEncryptByBase64(ctx context.Context, text string) (string, error) {
	encrypt, err := gaes.Encrypt([]byte(text), g.Cfg().MustGet(ctx, "jwt.encrypt.secret").Bytes(), g.Cfg().MustGet(ctx, "jwt.encrypt.iv").Bytes())
	if err != nil {
		return "", err
	}
	return string(gbase64.Encode(encrypt)), nil
}
func AesDecryptByBase64(ctx context.Context, cipherText string) (string, error) {
	src, err := gbase64.Decode([]byte(cipherText))
	decrypt, err := gaes.Decrypt(src, g.Cfg().MustGet(ctx, "jwt.encrypt.secret").Bytes(), g.Cfg().MustGet(ctx, "jwt.encrypt.iv").Bytes())
	if err != nil {
		return "", err
	}
	return string(decrypt), nil
}
